package net.chathu.airgw.packets.chat 
{
	import net.chathu.airgw.Config;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.Room;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.packets.gw.serverpackets.EndOfNamesPacket;
	import net.chathu.airgw.packets.gw.serverpackets.JoinPacket;
	import net.chathu.airgw.packets.gw.serverpackets.NamesPacket;
	import net.chathu.airgw.packets.gw.serverpackets.NickModePacket;
	import net.chathu.airgw.packets.gw.serverpackets.TopicPacket;
	import net.chathu.airgw.PrefixManager;
	/**
	 * ...
	 * @author ze
	 */
	public class IAddRooms extends ChatPacket
	{
		private var roomId:int;
		private var userId:int;
		public function IAddRooms() 
		{
			//>> iadd|rooms/136/users|586915|1|id|586915
			
		}
		
		override public function execute():void
		{
			var posA:int=String(data[1]).indexOf('/',0) +1;
			var posB:int=String(data[1]).indexOf('/', 7) ;

			roomId = parseInt(data[1].substring(posA, posB));
			userId = parseInt(data[2]);
			
			var user:User = User(sessionData.chatData.users.getUser(userId));
			if (user)
			{
				var room:Room = sessionData.chatData.rooms.getRoom(roomId);
				if (userId == sessionData.chatData.userId)
				{
					//en leptem be egy szobaba
					
					if (room) {
						//letezo szoba..
						//korabbi komment alapjan elkepzelheto, hogy duplan erkezik meg ez az uzenet:
						var roomInfo:JoinedRoomInfo = sessionData.chatData.getJoinedRoomInfo(roomId);
						
						//sendToClient(new JoinPacket(user.userHost,room.name));
						sendToClient(new JoinPacket(sessionData.yourHost,room.name));
						sendToClient(new TopicPacket(sessionData.nick, room.name, room.topic));
						//sendToClient(new TopicNameTimePacket(sessionData.nick,channel,"Chat.hu",System.currentTimeMillis()/1000));
						
						if (!roomInfo)
						{
							//beleptunk egy szobaba, ahol valoszinuleg nincs senki:
							roomInfo = new JoinedRoomInfo(roomId);
							sessionData.chatData.joinedRooms.push(roomInfo);
							
							
						} else {
							// beleptunk egy szobaba, ahol masok is vannak
							trace("Ebben a szobaban masok is vannak!");
						}
						var rights:int = -1;
						if (data[4] == "r") {
							rights = data[5];
						}
						roomInfo.addUser(user.id, rights); //sajat jogod?
						
						//ures listanal is kell ilyet kuldeni?
						sendToClient(new NamesPacket(sessionData.nick,roomInfo,room.name, sessionData));
						sendToClient(new EndOfNamesPacket(sessionData.nick,room.name));
						
					} else {
						trace("NonExisting room, id: " + roomId);
					}
					
				} else {
					//valaki mas lepett be
					
					//elvileg mar van errol infonk..
					var roomInfo2:JoinedRoomInfo = sessionData.chatData.getJoinedRoomInfo(roomId);
					
					if (roomInfo2)
					{
						roomInfo2.addUser(user.id,data[5]);
						
					} else {
						trace("Error, missing room info, id: " + roomId);
					}
					if (room)
					{
						sendToClient(new JoinPacket(user.userHost, room.name));
						var modes:String  = PrefixManager.getUserModes(sessionData, user, roomInfo2);
						if (modes.length > 0) {
							sendToClient(new NickModePacket(room.name, user.name, "+" + modes));
						}
					} else {
						trace("Missing room, roomId: " + roomId + " (IAddRooms)");
					}
				}
			} else {
				trace("Error, missing user, id: " + userId);
			}
		}
		
	}

}